package com.zz.framework.core.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.Log;
import android.view.Display;

import java.lang.reflect.Method;

/**
 * Copyright © 2021 StariverPool All rights reserved.
 * Created on 2021/11/4 12:38 下午.
 *
 * @version: V1.0
 * 禁用字体大小和显示大小随系统变化
 */
public class DispUtil {
    private static String TAG = "DispUtil";

    /**
     * 禁用显示大小改变和文字大小
     */
//    public static Resources disabledDisplayDpiChange(Resources res) {
//        Configuration newConfig = res.getConfiguration();
//        //字体非默认值
//        if (res.getConfiguration().fontScale != 1) {
//            newConfig.setToDefaults(); //设置默认
//            res.updateConfiguration(newConfig, res.getDisplayMetrics());
//        }
//        return res;
//    }


    /**
     * 禁用显示大小改变和文字大小
     */
    public static Resources disabledDisplayDpiChange(Resources res) {
        Configuration newConfig = res.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //字体非默认值
            if (res.getConfiguration().fontScale != 1) {
                newConfig.fontScale = 1;
            }
            newConfig.densityDpi = getDefaultDisplayDensity();
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        } else {
            //字体非默认值
            if (res.getConfiguration().fontScale != 1) {
                newConfig.fontScale = 1;//设置默认
                res.updateConfiguration(newConfig, res.getDisplayMetrics());
            }
        }
        return res;
    }

    /**
     * 保持字体大小不随系统设置变化（用在界面加载之前）
     * 要重写Activity的getResources()
     */
    public static Resources getResources(Context context, Resources resources, float fontScale) {
        Configuration config = resources.getConfiguration();
        Log.i(TAG, "changeActivityFontScaleR " + config.fontScale + ", " + fontScale);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (config.fontScale != fontScale) {
                config.fontScale = fontScale;
                resources = context.createConfigurationContext(config).getResources();
            }
        } else {
            if (config.fontScale != fontScale) {
                config.fontScale = fontScale;
                resources = context.createConfigurationContext(config).getResources();
            }
        }
        return resources;
    }

    /**
     * 获取手机出厂时默认的densityDpi
     */
    public static int getDefaultDisplayDensity() {
        try {
            Class aClass = Class.forName("android.view.WindowManagerGlobal");
            Method method = aClass.getMethod("getWindowManagerService");
            method.setAccessible(true);
            Object iwm = method.invoke(aClass);
            Method getInitialDisplayDensity = iwm.getClass().getMethod("getInitialDisplayDensity", int.class);
            getInitialDisplayDensity.setAccessible(true);
            Object densityDpi = getInitialDisplayDensity.invoke(iwm, Display.DEFAULT_DISPLAY);
            return (int) densityDpi;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }


    /**
     * 保持字体大小不随系统设置变化（用在界面加载之前）
     * 要重写Activity的attachBaseContext()
     */
    public static Context attachBaseContext(Context context, float fontScale) {
        Configuration config = context.getResources().getConfiguration();
        Log.i(TAG, "changeActivityFontScaleA " + config.fontScale + ", " + fontScale);
        //正确写法
        config.fontScale = fontScale;
        return context.createConfigurationContext(config);
    }


    /**
     * 保存字体大小，后通知界面重建，它会触发attachBaseContext，来改变字号
     */
    public static void recreate(Activity activity) {
        //只有这句才有效，其它两句都无效
        activity.recreate();
    }


}
